﻿#light

open System
open System.IO
open System.Linq

let parse (time: String) =
    TimeSpan.Parse(time.Replace(',', '.'))

let text time =
    let result = time.ToString().Replace('.', ',')
    
    result.Substring(0, result.IndexOf(',') + 4)

let delta = parse (System.Environment.GetCommandLineArgs().[1])

let inputStream =
    seq{ while true do yield Console.ReadLine() }

let lines = inputStream |> Seq.take_while (fun line -> line <> null)
let input = Seq.fold (fun (res: string) (l: string) -> String.Concat(res, l, "\n")) "" lines

let titles = input.Split([|"\n\n"|], StringSplitOptions.RemoveEmptyEntries)

let titlesLines = titles |> Array.map (fun title -> title.Split([|'\n'|]))

let fixTimes (line: String) =
    let times = line.Split(Array.create 1 " --> ", StringSplitOptions.RemoveEmptyEntries)
    let start = parse times.[0] + delta
    let fin = parse times.[1] + delta
    text start + " --> " + text fin

let fixTitleLines (lines: String array) =
    lines.[1] <- fixTimes lines.[1]
    lines

let result = Array.map fixTitleLines titlesLines
for titleLines in result do
    for titleLine in titleLines do
        Console.WriteLine(titleLine)
    
    Console.WriteLine()
